package tw.asap.plus.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;
import tw.asap.plus.entity.response.ShopIndex;
import tw.asap.plus.ui.activity.MerchantActivity;
import tw.asap.plus.utils.Utils;

/**
 * Description:
 * Created:      [15/11/11]
 * Author:       [Yang]
 **/
public class MerchantListAdapter extends RecyclerView.Adapter {

    private final Context context;
    private final List<ShopIndex.ShopEntity> shopList;

    public MerchantListAdapter(Context context, List<ShopIndex.ShopEntity> shopList) {
        this.context = context;
        this.shopList = shopList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_store, null);
        itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        ViewHolder itemHolder = new ViewHolder(itemView);
        itemHolder.setItemClickListener(() -> {
            if (itemHolder.getAdapterPosition() == RecyclerView.NO_POSITION) {
                return;
            }
            ShopIndex.ShopEntity shopEntity = shopList.get(itemHolder.getAdapterPosition());
            Intent intent = new Intent(context, MerchantActivity.class);
            intent.putExtra("SHOP_ENTITY", shopEntity);
            context.startActivity(intent);
        });
        itemView.setOnClickListener(itemHolder);
        return itemHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).setData(shopList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return shopList.size();
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'view_category.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @InjectView(R.id.store_image)
        SimpleDraweeView store_image;

        @InjectView(R.id.store_name)
        TextView store_name;

        @InjectView(R.id.ratingBar)
        RatingBar ratingBar;

        @InjectView(R.id.sales_volume)
        TextView sales_volume;

        @InjectView(R.id.delivery_info)
        TextView delivery_info;

        @InjectView(R.id.store_address)
        TextView store_address;

        ItemClickListener itemClickListener = null;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }

        public void setItemClickListener(ItemClickListener mListener) {
            this.itemClickListener = mListener;
        }

        public void setData(ShopIndex.ShopEntity entity) {
            store_image.setImageURI(Uri.parse(entity.getLogo()));
            store_name.setText(entity.getName().concat("(").concat(entity.getSubname()).concat(")"));
            ratingBar.setNumStars(Integer.parseInt(entity.getScore()));
            StringBuilder sb = new StringBuilder();
            sb.append("起送 $").append(Utils.getFormatAmt(entity.getShipping_base())).append(" | ").append("配送 $")
                    .append(Utils.getFormatAmt(entity.getShipping_price())).append(" | ").append("平均")
                    .append(entity.getShipping_times()).append("分钟");
            delivery_info.setText(sb.toString());
            double distance = Double.parseDouble(entity.getDistance());
            String distanceStr = entity.getDistance();
            if(distance < 1){
                double d = distance * 1000;
                distanceStr = String.valueOf(d);
            }else{
                distanceStr = distanceStr.concat("km");
            }
            store_address.setText("据您".concat(distanceStr).concat("  ").concat(entity.getAddress()));
            String saleInfo = "月销售".concat(entity.getSale_nums()).concat("份");
            sales_volume.setText(saleInfo);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onClick();
            }
        }
    }

    public interface ItemClickListener {
        void onClick();
    }
}
